<template>
  <div class="form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth">
      <el-row>
        <template v-for="item in formItems" :key="item.id">
          <el-col v-bind="colLayout"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item :label="item.label" :style="itemStyle">
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <!-- <el-input
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                   v-model="formData[`${item.field}`]"
                /> -->
                <!-- 此处的$event是可以拿到相应的字段的 -->
                <el-input
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                />
              </template>
              <!-- <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    style="width: 100%"
                    :model-value="modelValue[`${item.field}`]"
                    @update:modelValue="handleValueChange($event, item.field)"
                    >{{ option.title }}</el-option
                  >
                </el-select>
              </template> -->
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :model-value="modelValue[`${item.field}`]"
                  @update:modelValue="handleValueChange($event, item.field)"
                ></el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch, computed } from "vue";
import { IFormItem } from "../types";
export default defineComponent({
  props: {
    // 双向绑定传过来的值叫modelvalue
    modelValue: {
      type: Object,
      required: true
    },
    formItems: {
      type: Array as PropType<IFormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: "100px"
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: "10px 25px" })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6, // >1920px 4个
        lg: 6,
        md: 8,
        sm: 12,
        xs: 24
      })
    }
  },
  emits: ["update:modelValue"],
  setup(props, { emit }) {
    // const formData = ref({ ...props.modelValue });
    // watch(formData, (newValue) => emit("update:modelValue", newValue), {
    //   // 需要开启深度监视
    //   deep: true
    // });
    // return {
    //   formData
    // };
    // const formData = ref({ ...props.modelValue });
    // watch(
    //   formData,
    //   (newValue) => {
    //     console.log(newValue);
    //     emit("update:modelValue", newValue);
    //   },
    //   {
    //     deep: true
    //   }
    // );
    const handleValueChange = (value: any, filed: string) => {
      emit("update:modelValue", { ...props.modelValue, [filed]: value });
    };
    return {
      handleValueChange
    };
  }
});
</script>
<style scoped lang="less">
.form {
  padding: 20px;
  // padding-top: 30px;
  /deep/ el-form-item {
    display: flex;
    --font-size: 14px;
    margin-bottom: 0px;
  }
}
</style>
